﻿using System;
using EnvDTE80;
using System.Diagnostics.Contracts;
using EnvDTE;
using Microsoft.VisualStudio.CommandBars;

namespace MoXAML.Infrastructure
{
    public static class ActiveDocumentUtil
    {
        /// <summary>
        /// Has any text been selected?
        /// </summary>
        /// <param name="application">The application instance to retrieve the information from.</param>
        /// <returns>True if text has been selected, false otherwise.</returns>
        public static bool IsTextSelected(this DTE2 application)
        {
            if (application.ActiveDocument == null) return false;

            return !string.IsNullOrWhiteSpace(((TextSelection)application.ActiveDocument.Selection).Text);
        }

        /// <summary>
        /// Get the command bar.
        /// </summary>
        /// <param name="application">The application instance to retrieve the information from.</param>
        /// <param name="barName">The name of the command bar to retrieve.</param>
        /// <returns>The command bar.</returns>
        public static CommandBar FindCommandBar(this DTE2 application, string barName)
        {
            return ((Microsoft.VisualStudio.CommandBars.CommandBars)application.CommandBars)[barName];
        }
    }
}
